//
//  HDepartJDViewController.m
//  NursingManage
//
//  Created by Zhen Huang on 17/5/17.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "HDepartJDViewController.h"
#import "JYJConst.h"
#import "JYJTableCell.h"
#import "Model_Hr_Scheduling_Dept.h"
#import "Model_Hr_Scheduling.h"
#import "InitDbTable.h"
#import "HCommon.h"
#import "NSObject+AutoDescription.h"
@interface HDepartJDViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, copy) NSString *week_begin;
@property (nonatomic, copy) NSString *week_end;
@property (nonatomic, copy) NSString *Group_ID;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *widthArray;
@property (nonatomic, strong) NSArray *PeopleArray;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, assign) NSInteger week;
@property (nonatomic, strong) NSMutableArray *array1;
@property (nonatomic, strong) NSMutableArray *array2;
@property (nonatomic, strong) NSMutableDictionary *bigDictionary;
@property (nonatomic, strong) NSArray *weekArray;

@end

@implementation HDepartJDViewController
- (instancetype)initWithGroupID:(NSString *)Group_id
                    PeopleArray:(NSArray *)PeopleArray
                      dateArray:(NSArray *)dateArray
                     week_begin:(NSString *)week_begin
                       week_end:(NSString *)week_end
{
    if (self) {
        self.Group_ID = Group_id;
        self.dataArray = [NSMutableArray new];
        self.widthArray = [NSMutableArray new];
        self.array = [NSMutableArray new];
        self.PeopleArray = PeopleArray;
        self.week_begin = week_begin;
        self.week_end = week_end;
        self.array1 = [NSMutableArray new];
        self.array2 = (NSMutableArray *)dateArray;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.dateLabel];
    [self.view addSubview:self.tableView];
    [self setUpButtons];
    [self setupdata:self.PeopleArray];
    self.week = 0;
    self.dateLabel.text = [NSString stringWithFormat:@"%@ -%@",[self.week_begin substringFromIndex:5],[self.week_end substringFromIndex:5]];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight + 50, ScreenWidth, ScreenHeight - NavBarHeight - 50) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}
- (UILabel *)dateLabel
{
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(17, NavBarHeight + 20, 200, 20)];
        _dateLabel.font = [UIFont systemFontOfSize:16];
        
    }
    return _dateLabel;
}
- (void)setupdata1 :(NSArray *)arrays peopleArray :(NSArray *)peopleArray
{
    
    [self.widthArray removeAllObjects];
    
    NSArray *dataArray = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
    
    NSMutableArray *Array = [NSMutableArray new];
    
    
    [self.array2 insertObject:@"人员" atIndex:0];

    [Array addObject: self.array2];
    
    
    if (self.array2.count > 0) {
        
        
        NSMutableArray *big = [NSMutableArray new];
        
        for (int j = 0; j < dataArray.count; j ++) {
            
            NSMutableArray *DataArray = [NSMutableArray new];
            
            [DataArray addObject:dataArray[j]];
            
            [big addObject:DataArray];
            
            
            for (NSArray *smallArray in arrays) {
                
                [big[j] addObject:smallArray[j]];
                
            }
            if (DataArray.count < self.array2.count) {
                
                NSInteger count = (unsigned long)(self.array2.count - DataArray.count);
                
                for (int z = 0; z < count; z ++) {
                    
                    [DataArray addObject:@""];
                }
            }
            
            [Array addObject:DataArray];
            NSLog(@"arra%@  big=%@",Array,big);
            
        }
        for (int i= 0; i < dataArray.count +1 ; i ++) {
            
            [self.widthArray addObject:@100];
        }
        
    }else
    {
        
        [self alert:@"暂无排班!"];
        for (int i= 0; i < 1; i ++) {
            
            [self.widthArray addObject:@100];
        }
    }
    
    self.dataArray = [Array mutableCopy];
    [self.tableView reloadData];
    

}
- (void)setupdata :(NSArray *)arrays
{

    [self.widthArray removeAllObjects];
    
    NSArray *dataArray = @[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"];
    
    NSMutableArray *Array = [NSMutableArray new];
    
    [self.array2 insertObject:@"人员" atIndex:0];
    
    [Array addObject: self.array2];
    
    
    if (self.array2.count > 0) {
        
        
         NSMutableArray *big = [NSMutableArray new];

        for (int j = 0; j < dataArray.count; j ++) {
            
            NSMutableArray *DataArray = [NSMutableArray new];
            
            [DataArray addObject:dataArray[j]];
            
            [big addObject:DataArray];
            
//            NSArray *smallArray = arrays[j];
            
            
            for (NSArray *smallArray in arrays) {
                
                [big[j] addObject:smallArray[j]];

            }
                        if (DataArray.count < self.array2.count) {
                
                NSInteger count = (unsigned long)(self.array2.count - DataArray.count);
                
                for (int z = 0; z < count; z ++) {
                    
                    [DataArray addObject:@""];
                }
            }
            
            [Array addObject:DataArray];

        }
        for (int i= 0; i < dataArray.count +1 ; i ++) {
            
            [self.widthArray addObject:@100];
        }
        
    }else
    {
        
        [self alert:@"暂无排班!"];

        for (int i= 0; i < 1; i ++) {
            
            [self.widthArray addObject:@100];
        }
    }
    
    self.dataArray = [Array mutableCopy];
    [self.tableView reloadData];
    
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JYJTableCell *cell = [JYJTableCell cellWithTableView:tableView];
    cell.data = self.dataArray;
    cell.widthArray = self.widthArray;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *arr = [self.dataArray firstObject];
    return itemCellH * arr.count + 20;
    
}

- (void)setUpButtons
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(ScreenWidth - 80, NavBarHeight + 20, 25, 22.5);
    [button setImage:[UIImage imageNamed:@"icon_left2"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(left:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *buttonRight = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonRight.frame = CGRectMake(button.right + 20,button.top, 25, 22.5);
    [buttonRight setImage:[UIImage imageNamed:@"icon_rightSC"] forState:UIControlStateNormal];
    [buttonRight addTarget:self action:@selector(RIGHT:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
    [self.view addSubview:buttonRight];
    
}
- (void)left:(UIButton *)sender
{
    [self reduce:self.week];
    [self.array2 removeAllObjects];
    InitDbTable *manager = [[InitDbTable alloc]init];
    [InitDbTable sharedFMDBManeger];
    InitDbTable *managerSCD = [[InitDbTable alloc]init];
    [InitDbTable sharedFMDBManeger];
    self.weekArray = [manager getWeekWithTime:self.week_begin];
    //调用打开数据库方法
//    [manager dataDepartmentBase];
//    [managerSCD dataBaseSCD];
    NSMutableArray *array = [NSMutableArray new];
    array = [manager DepartmentModel:self.Group_ID :self.week_begin :self.week_end];
    for (Model_Hr_Scheduling_Dept *model in array) {
        [self.array2 addObject:model.EMP_NAME];
    }
    NSMutableArray *arraySCM = [NSMutableArray new];
    arraySCM = [managerSCD allModelSCDWithdate:self.Group_ID SCD_date:self.week_begin SCD_EndDATE:self.week_end];
    NSMutableArray *dateMutablearray = [@[] mutableCopy];
    
//    NSMutableArray *aaaaa =[NSMutableArray new];
//    for (int i = 0; i < array.count; i ++) {
//        
//        NSMutableArray *arra = [NSMutableArray new];
//        Model_Hr_Scheduling_Dept *model = array[i];
//        for (int j = 0; j < sameArray.count; j ++) {
//            Model_Hr_Scheduling *model1 = sameArray[j];
//            
//            if ([model1.EMP_ID isEqualToString:model.EMP_ID]) {
//                
//                if ([model1.CLASS_NAME isEqual:[NSNull null]]) {
//                    [arra addObject:@"11"];
//                }else {
//                    [arra addObject:model1.CLASS_NAME];
//                    
//                }
//            }
//            NSLog(@"arrrrrrrr=%@",arra);
//            
//        }
//        [aaaaa addObject:arra];
//    }
//    
//    for (int i = 0; i < sameArray.count; i ++) {
//        Model_Hr_Scheduling *model = sameArray[i];
//        NSString *string = model.EMP_ID;
//        Model_Hr_Scheduling_Dept *modeldept = array[i];
//        NSMutableArray *tempArray = [manager ModelSCDWithdate:self.Group_ID SCD_date:self.week_begin SCD_EndDATE:self.week_end EMP_ID:modeldept.EMP_ID];
//        for (int j = i+1; j < sameArray.count; j ++) {
//            Model_Hr_Scheduling *model = sameArray[j];
//            NSString *jstring = model.EMP_ID;
//            if([string isEqualToString:jstring]){
//                [sameArray removeObjectAtIndex:j];
//                j = j -1;
//            }
//        }
//        [dateMutablearray addObject:tempArray];
//    }
//    NSMutableArray *Array = [NSMutableArray new];
//    for (NSArray *arrays in dateMutablearray) {
//        NSMutableArray *array1 = [NSMutableArray new];
//        for (int i = 0; i < arrays.count; i ++) {
//            NSString *name = arrays[i];
//            [array1 addObject:name?:@""];
//        }
//        if (array1.count < 7) {
//            NSInteger count = (unsigned long)(7 - array1.count);
//            for (int j = 0; j < count; j ++) {
//                [array1 addObject:@""];
//            }
//        }
//        [Array addObject:array1];
//    }
//    if (Array.count < self.array2.count) {
//        NSInteger count = (unsigned long)(self.array2.count - Array.count);
//        
//        for (int i = 0; i < count; i ++) {
//            NSMutableArray *array = [NSMutableArray new];
//            for (int j = 0; j < 7; j ++) {
//                [array addObject:@""];
//            }
//            [Array addObject:array];
//        }
//    }
    
    NSMutableArray *sameArray = [NSMutableArray arrayWithArray:arraySCM];
    
    
    self.bigDictionary = [NSMutableDictionary new];
    
    for (int i = 0; i < sameArray.count; i++) {
        
        Model_Hr_Scheduling *schModel = sameArray[i];
        
        NSString *keySting = F(@"%@%@", schModel.SCD_DATE,schModel.EMP_ID);
        
        [self.bigDictionary setObject:schModel.CLASS_NAME forKey:keySting];
    }
    
    
    
    for (int i = 0; i < array.count; i ++) {
        
        NSMutableArray *SubArray = [NSMutableArray new];
        
        Model_Hr_Scheduling_Dept *model = array[i];
        
        for (int j = 0; j < self.weekArray.count; j ++) {
            
            NSString *key = F(@"%@%@",self.weekArray[j],model.EMP_ID);
            
            [SubArray addObject:self.bigDictionary[key]?:@""];
        }
        model.subArray = SubArray;
        
        [array replaceObjectAtIndex:i withObject:model];
    }
    
    NSMutableArray *bigArray = [NSMutableArray new];
    
    for (int i = 0; i < array.count; i ++) {
        
        Model_Hr_Scheduling_Dept *model = array[i];
        
        [bigArray addObject:model.subArray];
    }
    

    
    self.dateLabel.text = [NSString stringWithFormat:@"%@ -%@",[self.week_begin substringFromIndex:5],[self.week_end substringFromIndex:5]];
    [self setupdata1:bigArray peopleArray:self.array2];
    
}
- (void)RIGHT:(UIButton *)sender
{
    
    [self weekAdd:self.week];
    [self.array2 removeAllObjects];
    InitDbTable *manager = [[InitDbTable alloc]init];
    [InitDbTable sharedFMDBManeger];
    InitDbTable *managerSCD = [[InitDbTable alloc]init];
    [InitDbTable sharedFMDBManeger];
    self.weekArray = [manager getWeekWithTime:self.week_begin];
//    //调用打开数据库方法
//    [manager dataDepartmentBase];
//    [managerSCD dataBaseSCD];
    NSMutableArray *array = [NSMutableArray new];
    array = [manager DepartmentModel:self.Group_ID :self.week_begin :self.week_end];
    for (Model_Hr_Scheduling_Dept *model in array) {
        [self.array2 addObject:model.EMP_NAME];
    }
    NSMutableArray *arraySCM = [NSMutableArray new];
    arraySCM = [managerSCD allModelSCDWithdate:self.Group_ID SCD_date:self.week_begin SCD_EndDATE:self.week_end];
    
    NSMutableArray *sameArray = [NSMutableArray arrayWithArray:arraySCM];

    
    self.bigDictionary = [NSMutableDictionary new];
    
    for (int i = 0; i < sameArray.count; i++) {
        
        Model_Hr_Scheduling *schModel = sameArray[i];
        
        NSString *keySting = F(@"%@%@", schModel.SCD_DATE,schModel.EMP_ID);
        
        [self.bigDictionary setObject:schModel.CLASS_NAME forKey:keySting];
    }
    
    
    
    for (int i = 0; i < array.count; i ++) {
        
        NSMutableArray *SubArray = [NSMutableArray new];
        
        Model_Hr_Scheduling_Dept *model = array[i];
        
        for (int j = 0; j < self.weekArray.count; j ++) {
            
            NSString *key = F(@"%@%@",self.weekArray[j],model.EMP_ID);
            
            [SubArray addObject:self.bigDictionary[key]?:@""];
        }
        model.subArray = SubArray;
        
        [array replaceObjectAtIndex:i withObject:model];
    }
    
    NSMutableArray *bigArray = [NSMutableArray new];
    
    for (int i = 0; i < array.count; i ++) {
        
        Model_Hr_Scheduling_Dept *model = array[i];
        
        [bigArray addObject:model.subArray];
    }

    
//    NSMutableArray *dateMutablearray = [@[] mutableCopy];
//    NSMutableArray *sameArray = [NSMutableArray arrayWithArray:arraySCM];
//    for (int i = 0; i < sameArray.count; i ++) {
//        Model_Hr_Scheduling *model = sameArray[i];
//        NSString *string = model.EMP_ID;
//        NSMutableArray *tempArray1 = [@[] mutableCopy];
//        NSMutableArray *tempArray = [@[] mutableCopy];
//        [tempArray addObject:model];
//        [tempArray1 addObject:model.CLASS_NAME];
//        for (int j = i+1; j < sameArray.count; j ++) {
//            Model_Hr_Scheduling *model = sameArray[j];
//            NSString *jstring = model.EMP_ID;
//            if([string isEqualToString:jstring]){
//                [tempArray addObject:model];
//                [tempArray1 addObject:model.CLASS_NAME];
//                [sameArray removeObjectAtIndex:j];
//                j = j -1;
//            }
//        }
//        [dateMutablearray addObject:tempArray1];
//        
//    }
//    NSMutableArray *Array = [NSMutableArray new];
//    for (NSArray *arrays in dateMutablearray) {
//        NSMutableArray *array1 = [NSMutableArray new];
//        for (int i = 0; i < arrays.count; i ++) {
//            NSString *name = arrays[i];
//            [array1 addObject:name?:@""];
//        }
//        if (array1.count < 7) {
//            NSInteger count = (unsigned long)(7 - array1.count);
//            for (int j = 0; j < count; j ++) {
//                [array1 addObject:@""];
//            }
//        }
//        [Array addObject:array1];
//    }
//    if (Array.count < self.array2.count) {
//        NSInteger count = (unsigned long)(self.array2.count - Array.count);
//        for (int i = 0; i < count; i ++) {
//            NSMutableArray *array = [NSMutableArray new];
//            for (int j = 0; j < 7; j ++) {
//                [array addObject:@""];
//            }
//            [Array addObject:array];
//        }
//    }
    
    self.dateLabel.text = [NSString stringWithFormat:@"%@ -%@",[self.week_begin substringFromIndex:5],[self.week_end substringFromIndex:5]];
    [self setupdata1:bigArray peopleArray:self.array2];
    
}
#pragma 获得之后几周每周一 周日的日期
- (void)weekAdd :(NSInteger)week_add
{
    week_add++;
    self.week = week_add;
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitDay fromDate:now];
    NSInteger weekDay = [comp weekday];
    NSInteger day = [comp day];
    long firstDiff,lastDiff;
    if (weekDay == 1) {
        firstDiff = 1;
        lastDiff = 0;
    }else{
        firstDiff = [calendar firstWeekday] - weekDay + self.week *7;
        lastDiff = 9 - weekDay + self.week * 7;
    }
    
    // 在当前日期(去掉了时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    [lastDayComp setDay:day + lastDiff-1];
    NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd"];
    self.week_begin = [formater stringFromDate:firstDayOfWeek];
    self.week_end = [formater stringFromDate:lastDayOfWeek];
    
}
#pragma 获得之前几周每周一 周日的日期
- (void)reduce :(NSInteger)week_reduce
{
    week_reduce--;
    self.week = week_reduce;
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday|NSCalendarUnitDay fromDate:now];
    
    // 得到星期几
    // 1(星期一) 2(星期二) 3(星期三) 4(星期四) 5(星期五) 6(星期六) 7(星期天)
    NSInteger weekDay = [comp weekday];
    // 得到几号
    NSInteger day = [comp day];
    // 计算当前日期和这周的星期一和星期天差的天数
    long firstDiff,lastDiff;
    if (weekDay == 1) {
        firstDiff = 1;
        lastDiff = 0;
    }else{
        firstDiff = [calendar firstWeekday] - weekDay + self.week *7;
        lastDiff = 9 - weekDay + self.week * 7;
    }
    
    //    firstDiff = [calendar firstWeekday] - weekDay + self.week *7;
    //    lastDiff = 9 - weekDay + self.week *7;
    // 在当前日期(去掉了时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek= [calendar dateFromComponents:firstDayComp];
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
    [lastDayComp setDay:day + lastDiff -1];
    NSDate *lastDayOfWeek= [calendar dateFromComponents:lastDayComp];
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd"];
    self.week_begin = [formater stringFromDate:firstDayOfWeek];
    self.week_end = [formater stringFromDate:lastDayOfWeek];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
